package com.wl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wl.entity.FunctionTree;
import com.wl.mapper.FunctionTreeMapper;
import com.wl.service.FunctionTreeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class FunctionTreeServiceImpl extends ServiceImpl<FunctionTreeMapper, FunctionTree> implements FunctionTreeService {
    @Override
    public int updateTree(Long parentId, List<FunctionTree> trees) {
//        FunctionTree root = setChild(trees);
        FunctionTree root = null;

        root.setParentId(parentId);
        return updateById(root) ? 1:0;
    }

    @Override
    public int addTree(Long parentId, String name) {
        FunctionTree functionTree = new FunctionTree();
        functionTree.setParentId(parentId);
        functionTree.setName(name);
        return save(functionTree) ? 1 : 0;
    }

    @Override
    public List<FunctionTree> tree() {
        log.info("获取树请求---------");
        List<FunctionTree> list = list();
        List<FunctionTree> parentNode = list.stream().filter(f -> Objects.isNull(f.getParentId()))
                .toList();

        parentNode.forEach(function -> {
            function.setPath(function.getName());
            setChild(list,function);
        });

        
        
        return parentNode;
    }

    @Override
    public List<FunctionTree> query(String name) {
        //查询数据库目标
        List<FunctionTree> targets = lambdaQuery()
                .like(FunctionTree::getName, name)
                .list();

        // 存储目标，和目标的父节点
        List<FunctionTree> result = new ArrayList<>();
        // 写入所有查询的目标节点
        if(CollectionUtils.isNotEmpty(targets)){
            result.addAll(targets);
        }else {
            return null;
        }

        //查询树的所有
        List<FunctionTree> list = list();

        // 获取查询目标不是根节点的(需要获取父节点)
        List<FunctionTree> trees = targets.stream().filter(f -> Objects.nonNull(f.getParentId())).toList();
        trees.forEach(f -> getParentList(list,f,result));

        //获取临时存储目标是父节点的
        List<FunctionTree> parents = result.stream().filter(f -> Objects.isNull(f.getParentId())).toList();

        // 生成树
        parents.forEach(f -> {
            f.setPath(f.getName());
            setChild(result,f);
        });
        return parents;
    }


    /***
     * 递归获取某个节点的所有父节点
     * @param list
     * @param tree
     * @param result
     */
    private void getParentList(List<FunctionTree> list,FunctionTree tree,List<FunctionTree> result){
        list.stream().filter(f -> f.getId().equals(tree.getParentId())).findFirst()
                .ifPresent(f -> {
                    if(result.stream().noneMatch(f::equals)){
                        result.add(f);
                    }
                    if(Objects.nonNull(f.getParentId())){
                        getParentList(list,f,result);
                    }
                });

    }



    private void setChild(List<FunctionTree> trees,FunctionTree tree){
        List<FunctionTree> children = trees.stream().filter(f -> Objects.nonNull(f.getParentId())).filter(f -> f.getParentId().equals(tree.getId()))
                .peek(f -> f.setPath(tree.getPath() + "/" + f.getName()))
                .toList();

        if(CollectionUtils.isNotEmpty(children)){
            tree.setChild(children);
        }
        children.forEach(f -> setChild(trees,f));
    }
}
